package com.hllxd.hism.passport.login.gateway.config.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 *  使用时对应的Redis进行底层数据的缓存处理
 * @author  lixy
 * @date 16/10/18
 * */
@Component
public class RedisAdapter {
  private static final Logger logger = LoggerFactory.getLogger(RedisAdapter.class);
  @Autowired private RedisTemplate<String, Object> redisTemplate;

  @Autowired(required = false)
  @SuppressWarnings("unchecked")
  public void setRedisTemplate(RedisTemplate redisTemplate) {
    RedisSerializer stringSerializer = new StringRedisSerializer();
    redisTemplate.setKeySerializer(stringSerializer);
    redisTemplate.setValueSerializer(stringSerializer);
    redisTemplate.setHashKeySerializer(stringSerializer);
    redisTemplate.setHashValueSerializer(stringSerializer);

    this.redisTemplate = redisTemplate;
  }

  public Long rpush(String key, String data) {
    return redisTemplate.opsForList().rightPush(key, data);
  }

  public void del(String key) {
    redisTemplate.delete(key);
  }

  public Set<String> keys(String pattern) {
    return redisTemplate.keys(pattern);
  }

  public Long sadd(String key, String data) {
    return redisTemplate.opsForSet().add(key, data);
  }

  public Long sdel(String key, String data) {
    return redisTemplate.opsForSet().remove(key, data);
  }

  public Set<Object> members(String key) {
    // redisTemplate.opsForSet().intersect() // 交集
    // redisTemplate.opsForSet().union() // 合集
    return redisTemplate.opsForSet().members(key);
  }

  public Long zcard(String key) {
    return redisTemplate.opsForZSet().zCard(key);
  }

  public Object hget(String key, String data) {
    return redisTemplate.opsForHash().get(key, data);
  }

  public Long hincre(String key, long l) {
    return redisTemplate.opsForValue().increment(key, l);
  }

  public Object hincreget(String key) {
    return redisTemplate.opsForValue().get(key);
  }

  public void hset(String key, Object field, Object data) {
    redisTemplate.opsForHash().put(key, field, data);
  }

  public void hdel(String key, Object field) {
    redisTemplate.opsForHash().delete(key, field);
  }

  public Map<Object, Object> hgetall(String key) {
    return redisTemplate.opsForHash().entries(key);
  }

  public void batchDel(Set set, String field) {
    Iterator<String> it = set.iterator();
    while (it.hasNext()) {
      String keyStr = it.next();
      hdel(keyStr, field);
    }
  }

  public void batchsDel(String key, Set set) {
    Iterator<String> it = set.iterator();
    while (it.hasNext()) {
      String data = it.next();
      sdel(key, data);
    }
  }

  public Boolean setExpireTime(String key, Long timeOut) {
    return redisTemplate.expire(key, timeOut, TimeUnit.HOURS);
  }

  public Boolean setExpireAtTime(String key, Date date) {
    return redisTemplate.expireAt(key, date);
  }

  public void setValue(String key, String value, Long millisecond) {
    redisTemplate.opsForValue().set(key, value, millisecond, TimeUnit.MILLISECONDS);
  }

  public String getValue(String key) {
    Object value = redisTemplate.opsForValue().get(key);
    if (null == value) {
      return null;
    }
    return value.toString();
  }

  public void delKey(String key) {
    redisTemplate.delete(key);
  }
}
